package com.tmsps.common.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.tmsps.common.config.prop.Ne4CommonProperties;
import com.tmsps.common.filter.JFilterHttpMessageConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

/**
 * jackson配置
 * @author jiangtao
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(Ne4CommonProperties.class)
public class JacksonAutoConfiguration {

    private static final String FILTER_ID = "nyfilter";

    @Bean
    @ConditionalOnMissingBean(name = "mappingJackson2HttpMessageConverter")
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(Ne4CommonProperties commonProperties, ObjectMapper objectMapper) {
        if(commonProperties.getJfilter().getEnabled()){
            JFilterHttpMessageConverter messageConverter = new JFilterHttpMessageConverter(objectMapper);
            return messageConverter;
        }else {
            MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(objectMapper);
            return messageConverter;
        }
    }

    @Bean
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper getObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        Set<String> properties = new HashSet<>();
        properties.add("pk");
        properties.add("tableName");

        SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider().setFailOnUnknownId(false);
        FilterProvider filters = simpleFilterProvider.addFilter(FILTER_ID, SimpleBeanPropertyFilter.serializeAllExcept(properties));
        objectMapper.setFilterProvider(filters);

        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        simpleModule.addSerializer(Double.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Double.TYPE, ToStringSerializer.instance);
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        objectMapper.registerModule(simpleModule);
        // 反序例时，对于不在于返回对象中的字段，不进行映射
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return objectMapper;
    }
}
